package com.lv.service.weidget.canlender;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lv.service.R;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DayAdapter extends BaseAdapter {

    private List<DayBean> list;
    private Context context;
    private DataItemOnClickInterface itemOnClickInterface;
//    private String dataFristTime = "";
//    private String dataLastTime = "";
//    private boolean isFrist = true;

    public DayAdapter(List<DayBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public DayBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View view, ViewGroup parent) {

        ViewHolder viewHolder;
        if (null == view) {
//            view = LayoutInflater.from(context).inflate(R.layout.item_registrationadatper, null);
            view = View.inflate(context, R.layout.item_registrationadatper, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        Calendar calendar = Calendar.getInstance();
        int mYEAR = calendar.get(Calendar.YEAR);// 获取当前年以（0开头）
        int mMonth = calendar.get(Calendar.MONTH) + 1;// 获取当前月份以（0开头）
        int mDay = calendar.get(Calendar.DAY_OF_MONTH);// 获取当前日以（0开头）
        DayBean bean = getItem(position);
        viewHolder.day.setText(bean.getDay() + "");
        viewHolder.day.setGravity(Gravity.CENTER);

        viewHolder.day.setTypeface(Typeface.DEFAULT_BOLD);

        final String dataTime = bean.getYear() + "/" + bean.getMonth() + "/" + bean.getDay();
        int days = DateAndTimeUtil.getDays(bean.getYear(), bean.getMonth());

//        viewHolder.day.setTextColor(context.getResources().getColor(R.color.starttheme));


        //今日
        if (bean.isCurrentDay() && bean.getItemState() == DayBean.ITEM_STATE_NORMAL) {
            viewHolder.day_point.setVisibility(View.VISIBLE);
        } else {
            viewHolder.day_point.setVisibility(View.GONE);
        }
        //只显示当前月的数据
        if (bean.isCurrentMonth()) {
            viewHolder.day.setVisibility(View.VISIBLE);
            viewHolder.rl_item.setVisibility(View.VISIBLE);
        } else {
            viewHolder.day.setVisibility(View.GONE);
            viewHolder.rl_item.setVisibility(View.GONE);
        }
        viewHolder.sr_tv.setVisibility(View.GONE);
        viewHolder.sr_left.setVisibility(View.GONE);
        viewHolder.sr_right.setVisibility(View.GONE);
        //设置item状态
        if (bean.getItemState() == DayBean.ITEM_STATE_BEGIN_DATE) {
            //开始日期
            if (bean.isSelectedFinsh() && position % 7 == 6) {
                viewHolder.day.setBackgroundResource(com.lv.common.R.drawable.bg_4_ef7b4a);
            } else if (bean.isSelectedFinsh() && position % 7 == 0) {
//                viewHolder.day.setBackgroundResource(R.drawable.bg_left_data_selected);
                viewHolder.sr_left.setBackgroundResource(com.lv.common.R.drawable.bg_left_data_selected);
                viewHolder.sr_right.setVisibility(View.GONE);
                viewHolder.sr_tv.setVisibility(View.GONE);
                viewHolder.sr_left.setVisibility(View.VISIBLE);
            } else if (bean.isSelectedFinsh()) {
//                viewHolder.day.setBackgroundResource(R.drawable.bg_left_data_selected);
                viewHolder.sr_tv.setBackgroundResource(com.lv.common.R.drawable.bg_left_data_selected);
                viewHolder.sr_right.setVisibility(View.GONE);
                viewHolder.sr_left.setVisibility(View.GONE);
                viewHolder.sr_tv.setVisibility(View.VISIBLE);
            } else {
                viewHolder.day.setBackgroundResource(com.lv.common.R.drawable.bg_4_ef7b4a);
            }
            viewHolder.day.setTextColor(Color.WHITE);

        } else if (bean.getItemState() == DayBean.ITEM_STATE_END_DATE) {

            //结束日期
            if (bean.isSelectedFinsh() && bean.getDay() == 1) {
                viewHolder.day.setBackgroundResource(com.lv.common.R.drawable.bg_4_ef7b4a);
            } else if (bean.isSelectedFinsh() && position % 7 == 0) {
                viewHolder.day.setBackgroundResource(com.lv.common.R.drawable.bg_4_ef7b4a);
            } else if (bean.isSelectedFinsh() && position % 7 == 6) {
                viewHolder.sr_right.setBackgroundResource(com.lv.common.R.drawable.bg_right_data_selected);
                viewHolder.sr_left.setVisibility(View.GONE);
                viewHolder.sr_tv.setVisibility(View.GONE);
                viewHolder.sr_right.setVisibility(View.VISIBLE);
            } else if (bean.isSelectedFinsh()) {
                viewHolder.sr_tv.setBackgroundResource(com.lv.common.R.drawable.bg_right_data_selected);
                viewHolder.sr_left.setVisibility(View.GONE);
                viewHolder.sr_right.setVisibility(View.GONE);
                viewHolder.sr_tv.setVisibility(View.VISIBLE);
            } else {
                viewHolder.day.setBackgroundResource(com.lv.common.R.drawable.bg_4_ef7b4a);

            }
            viewHolder.day.setTextColor(Color.WHITE);

        } else if (bean.getItemState() == DayBean.ITEM_STATE_SELECTED) {
            //选中状态
            viewHolder.day.setTextColor(Color.WHITE);
            if (bean.getDay() == 1 && position % 7 != 0) {
                viewHolder.sr_tv.setBackgroundResource(com.lv.common.R.drawable.bg_left_data_selected);
                viewHolder.sr_tv.setVisibility(View.VISIBLE);
                viewHolder.sr_right.setVisibility(View.GONE);
                viewHolder.sr_left.setVisibility(View.GONE);
            } else if (position % 7 == 0) {//第一星期日和当前月第一天
//                viewHolder.day.setBackgroundResource(R.drawable.bg_left_data_selected);
                viewHolder.sr_left.setBackgroundResource(com.lv.common.R.drawable.bg_left_data_selected);
                viewHolder.sr_left.setVisibility(View.VISIBLE);
                viewHolder.sr_right.setVisibility(View.GONE);
                viewHolder.sr_tv.setVisibility(View.GONE);
            } else if (position % 7 == 6 || bean.getDay() == days) {//第一星期六和当前月最后一天
//                viewHolder.day.setBackgroundResource(R.drawable.bg_right_data_selected);
                viewHolder.sr_right.setBackgroundResource(com.lv.common.R.drawable.bg_right_data_selected);
                viewHolder.sr_left.setVisibility(View.GONE);
                viewHolder.sr_right.setVisibility(View.VISIBLE);
                viewHolder.sr_tv.setVisibility(View.GONE);
            } else {
//                viewHolder.day.setBackgroundResource(R.drawable.bg_center_selected_data);
                viewHolder.sr_tv.setBackgroundResource(com.lv.common.R.drawable.bg_center_selected_data);
                viewHolder.sr_tv.setVisibility(View.VISIBLE);
                viewHolder.sr_left.setVisibility(View.GONE);
                viewHolder.sr_right.setVisibility(View.GONE);
            }

        } else {
            //正常状态
//            viewHolder.day.setTextColor(context.getResources().getColor(R.color.color_242424));
            viewHolder.day.setBackgroundResource(com.lv.common.R.drawable.bg_trans);
            viewHolder.sr_tv.setBackgroundResource(com.lv.common.R.drawable.bg_trans);
            String currentTime = mYEAR + "/" + mMonth + "/" + mDay;
            boolean dateOneBigger = DateAndTimeUtil.isDateOneBigger(dataTime, currentTime);
            if (dateOneBigger) {
//            viewHolder.day.setBackgroundResource(R.drawable.redtheme_cyc_bg);
                viewHolder.day.setTextColor(context.getResources().getColor(com.lv.common.R.color.color_aaa));
            } else {
                viewHolder.day.setTextColor(context.getResources().getColor(com.lv.common.R.color.color_242424));
//            viewHolder.day.setBackgroundResource(R.drawable.gary_cyc_bg);

            }
        }

        viewHolder.rl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (itemOnClickInterface != null) {
                    itemOnClickInterface.onItemClickListener(dataTime, bean);
                }
            }
        });

        // 返回 view 或 textView 都行，因为都是同一个对象
        return view;
    }


    static class ViewHolder {
        //        @BindView(R.id.day)
        public TextView day;
        //        @BindView(R.id.day_point)
        TextView day_point;
        //        @BindView(R.id.sr_tv)
        View sr_tv;
        //        @BindView(R.id.sr_left)
        View sr_left;
        //        @BindView(R.id.sr_right)
        View sr_right;
        //        @BindView(R.id.rl_item)
        RelativeLayout rl_item;


        ViewHolder(View view) {
            day = view.findViewById(R.id.tv_day);
            day_point = view.findViewById(R.id.day_point);
            sr_tv = view.findViewById(R.id.sr_tv);
            sr_left = view.findViewById(R.id.sr_left);
            sr_right = view.findViewById(R.id.sr_right);
            rl_item = view.findViewById(R.id.rl_item);
//            ButterKnife.bind(this, view);
        }
    }


    //定义回调接口
    public interface DataItemOnClickInterface {
        //listView item下的控件点击事件（注意传入参数V）
        public void onItemClickListener(String data, DayBean bean);
    }

    public void setOnDataItemOnClickInterface(DataItemOnClickInterface dataItemOnClickInterface) {
        this.itemOnClickInterface = dataItemOnClickInterface;
    }
}
